package com.sai.transacciones;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class ConexionBD 
{
    private static ConexionBD instancia = null; //Regla del patrón
    private static String base, user, pass;
    private Connection conexion;
    private Statement sentencia;
    
    private ConexionBD(String b,String u,String p) throws ClassNotFoundException// Regla songleton constructor privado
    {
        ConexionBD.base = b;
        ConexionBD.user = u;
        ConexionBD.pass = p;
        Class.forName("com.mysql.jdbc.Driver");
    }
    
    public static ConexionBD getInstancia(String b,String u,String p) throws ClassNotFoundException
    {
        if(instancia==null){
            instancia = new ConexionBD(b,u,p);
        }else{
            ConexionBD.base=b;
            ConexionBD.user=u;
            ConexionBD.pass=p;
        }
        return instancia;
    }
    
    public boolean OpenCon()
    {
        String url="jdbc:mysql://127.0.0.1:3306/"+base;
        try{
            conexion = DriverManager.getConnection(url,user,pass);
            sentencia = conexion.createStatement();
        }catch(SQLException e){//JOptionPane.showMessageDialog(null, "error "+e);
            return false;
        }
        return true;
    }
    
    @Override
    protected void finalize() throws Throwable
    {
        sentencia.close();
        conexion.close();
        super.finalize();
    }
    
    public Statement getSentencia()
    {
        return sentencia;
    }
    
    
    //throws no maneja excepciones y try catch si maneja excepciones
}//Clase
